#pragma once

#include <cstring>
#include <dirent.h>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

namespace xict_common {
    namespace perception {
        namespace common {
            static inline int
            ReadFilesInDir(const char* p_dir_name,
                           std::vector<std::string>& file_names) {
                DIR* p_dir = opendir(p_dir_name);
                if (p_dir == nullptr) {
                    return -1;
                }

                struct dirent* p_file = nullptr;
                while ((p_file = readdir(p_dir)) != nullptr) {
                    if (strcmp(p_file->d_name, ".") != 0 &&
                        strcmp(p_file->d_name, "..") != 0) {
                        // std::string cur_file_name(p_dir_name);
                        // cur_file_name += "/";
                        // cur_file_name += p_file->d_name;
                        std::string cur_file_name(p_file->d_name);
                        file_names.push_back(cur_file_name);
                    }
                }

                closedir(p_dir);
                return 0;
            }

            // Function to trim leading and trailing whitespace from a string
            static inline std::string
            TrimLeadingWhitespace(const std::string& str) {
                size_t first = str.find_first_not_of(' ');
                if (std::string::npos == first) {
                    return str;
                }
                size_t last = str.find_last_not_of(' ');
                return str.substr(first, (last - first + 1));
            }

            // Src: https://stackoverflow.com/questions/16605967
            static inline std::string ToStringWithPrecision(const float a_value,
                                                            const int n = 2) {
                std::ostringstream out;
                out.precision(n);
                out << std::fixed << a_value;
                return out.str();
            }

            static inline int
            ReadLabels(const std::string labels_filename,
                       std::unordered_map<int, std::string>& labels_map) {
                std::ifstream file(labels_filename);
                // Read each line of the file
                std::string line;
                int index = 0;
                while (std::getline(file, line)) {
                    // Strip the line of any leading or trailing whitespace
                    line = TrimLeadingWhitespace(line);

                    // Add the stripped line to the labels_map, using the loop
                    // index as the key
                    labels_map[index] = line;
                    index++;
                }
                // Close the file
                file.close();

                return 0;
            }
        }   // namespace common
    }       // namespace perception
}   // namespace xict_common